﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using com.lover.astd.common.config;
using com.lover.astd.common.logic;
using com.lover.astd.common.manager;
using com.lover.astd.common.model;

namespace com.lover.astd.common.logicexe
{
    public abstract class ExeBase : IExecute
    {
        protected bool _ready = false;
        public bool Ready { get { return _ready; } }
        protected ILogger _logger;
        protected ProtocolMgr _proto;
        protected User _user;
        protected GameConfig _conf;
        private long _nextExeTimeStamp;
        /// <summary>
        /// 设置下一次启动时间
        /// </summary>
        public void setNextExeTime(long timestamp) { _nextExeTimeStamp = timestamp + TimeMgr.TimeStamp; }
        /// <summary>
        /// 下一次启动时间
        /// </summary>
        public long getNextExeTime() { return _nextExeTimeStamp; }
        public void setName(string name) { _name = name; }
        public string getName() { return _name; }
        public void setReadableName(string rname) { _readable = rname; }
        public string getReadableName() { return _readable; }
        protected void refreshUi() { _user.addUiToQueue(_name); }
        public void setVariables(ProtocolMgr proto, ILogger logger, User u, GameConfig conf) { _proto = proto; _logger = logger; _user = u; _conf = conf; }
        public virtual void init_data() { refreshUi(); }
        public abstract long execute();
        
        protected string _name = "";
        protected string _readable = "";
        protected Color _logColor = System.Drawing.Color.Black;
        protected virtual void logInfo(string text) { _logger.log(text, _logColor); }
        protected Dictionary<string, string> getConfig() { return _conf.getConfig(_name); }

        protected bool inJingyingArmyTime()
        {
            int hour = TimeMgr.DateTimeNow.Hour;
            if (hour == 13 || (hour >= 20 && hour < 23)) return true;
            return false;
        }
        protected long immediate() { return 2000; }
        /// <summary>
        /// 半小时执行一次
        /// </summary>
        /// <returns></returns>
        protected long next_halfhour()
        {
            // 以下几个函数除了forever外都需要算出具体的时间戳
            DateTime dtnow = TimeMgr.DateTimeNow;
            int offset = 0;
            if (dtnow.Minute < 30) offset = 30 - dtnow.Minute;
            else offset = 60 - dtnow.Minute;
            return offset * 60 * 1000;
        }
        /// <summary>
        /// 1小时执行一次
        /// </summary>
        /// <returns></returns>
        protected long next_hour()
        {
            // 以下几个函数除了forever外都需要算出具体的时间戳
            DateTime dtnow = TimeMgr.DateTimeNow;
            int offset = 60 - dtnow.Minute;
            return offset * 60 * 1000;
        }
        /// <summary>
        /// 1小时后
        /// </summary>
        /// <returns></returns>
        protected long an_hour_later()
        {
            return 60 * 60 * 1000;
        }
        protected long next_day()
        {
            // 5:30
            DateTime dtnow = TimeMgr.DateTimeNow;
            int offset_minute = 0, offset_hour = 0;
            if (dtnow.Hour < 5) offset_hour = 5 - dtnow.Hour - 1;
            else offset_hour = 24 + 5 - dtnow.Hour - 1;
            offset_minute = 60 - dtnow.Minute;
            return (offset_hour * 60 * 60 + (offset_minute + 30) * 60) * 1000;
        }
        protected long next_dinner()
        {
            //10:01 19:01
            DateTime dtnow = TimeMgr.DateTimeNow;
            int offset_hour = 0, offset_minute = 0;
            if (dtnow.Hour < 10) offset_hour = 10 - dtnow.Hour - 1;
            else if (dtnow.Hour < 19) offset_hour = 19 - dtnow.Hour - 1;
            else offset_hour = 24 + 10 - dtnow.Hour - 1;
            offset_minute = 60 - dtnow.Minute;
            return (offset_hour * 60 * 60 + (offset_minute + 1) * 60) * 1000;
        }
        protected bool in_gongjian_time()
        {
            // 10:00-10:30 / 15:00-15:30 / 20:30-21:00
            DateTime dtnow = TimeMgr.DateTimeNow;
            int hour = dtnow.Hour, minute = dtnow.Minute;
            if (hour == 10 && minute < 30) return true;
            if (hour == 15 && minute < 30) return true;
            if (hour == 20 && minute > 30) return true;
            return false;
        }
        protected long smaller_time(long time1, long time2)
        {
            if (time1 < time2) return time1;
            else return time2;
        }
        protected int getGoldAvailable()
        {
            return getAvailable("gold_reserve", _user.Gold);
        }
        protected int getSilverAvailable()
        {
            return getAvailable("silver_reserve", _user.Silver);
        }
        protected int getCreditAvailable()
        {
            return getAvailable("credit_reserve", _user.Credit);
        }
        protected int getStoneAvailable()
        {
            return getAvailable("stone_reserve", _user.Stone);
        }
        private int getAvailable(string global_key, int valueTotal)
        {
            Dictionary<string, string> gconfs = _conf.getConfig(ConfigStrings.S_Global);
            int _reserve = 0;
            if (gconfs.ContainsKey(global_key))
            {
                Int32.TryParse(gconfs[global_key], out _reserve);
            }
            int avail = valueTotal - _reserve;
            if (avail < 0) avail = 0;
            return avail;
        }
        protected double getGemPrice()
        {
            double gem_price = 100;
            Dictionary<string, string> gconfs = _conf.getConfig(ConfigStrings.S_Global);
            if(gconfs.ContainsKey(ConfigStrings.gem_price)) double.TryParse(gconfs[ConfigStrings.gem_price], out gem_price);
            if (gem_price > 0) gem_price = 10 / gem_price;
            else gem_price = 0.1;
            return gem_price;
        }
        protected List<int> generateIds(string id_string)
        {
            List<int> idstrs_to = new List<int>();
            if (id_string == null || id_string == "") return idstrs_to;
            string[] idstrs = id_string.Split(',');
            foreach (string strid in idstrs)
            {
                if (strid == null || strid == "") continue;
                int qhid = 0;
                Int32.TryParse(strid, out qhid);
                if (qhid == 0) continue;
                idstrs_to.Add(qhid);
            }
            return idstrs_to;
        }
    }
}
